<?php
namespace app\api\controller\product;

use app\BaseController;
use learn\services\UtilService as Util;
use app\models\product\Loan;
use app\Request;


//引入模型
use app\models\user\User;

class Product extends BaseController
{
    //产品列表
    public function index (Request $request) 
    {
        $where = Util::postMore([
            ['sort',""],
            ['quota_type',0],
            ['product_search',0],
            ['page',1],
            ['limit',20]
        ]);
        $uid = $request->uid();
        $search = productSeach();
        $list = Loan::pageList($where,$uid);
        
        return app("json")->success(compact('search','list'));
    }

    //推荐产品
    public function recommend (Request $request) 
    {
        $uid = $request->uid();
        //获取用户信息
        $useInfo = User::alias("u")->leftJoin("user_basic b",'b.uid=u.uid')->where("u.uid",$uid)->field("b.*,u.phone,u.realname,u.id_card,u.monthly_income,u.accumulation,u.occupation,u.company_phone,u.industry,u.bank_card,u.borrowing,u.bank_phone,u.liabilities,u.job_time,u.company_type,u.residence_info,u.add_job_time")->find()->toArray();
        //查看符合条件的产品
        $list = Loan::recommend($useInfo);
        return app("json")->success(compact('list'));
    }

    //获取产品详情
    public function productInfo(Request $request)
    {
        $where = Util::postMore([
            ['id',0],
        ]);
        //判断是否有id
        if($where['id'] == 0) return app("json")->fail("产品不存在");
        $info = Loan::alias("l")->leftJoin("loan_attr a","a.loan_id = l.id")->where("l.id",$where['id'])->find();
        //判断是否有数据
        if($info) $info->toArray();
        //处理期限&&利率
        $info['info'] = explode("\r\n",$info['info']);

        if (isset($info['term']) && isset($info['interest'])) {
            $term = explode(",",$info['term']);
            $interest = explode(",",$info['interest']);
            $correspond = array();
            for ($i=0;$i<count($term);$i++) {
                $correspond[$i]['term'] = $term[$i];
                $correspond[$i]['interest'] = $interest[$i];
            }
            $info['correspond'] = $correspond;


        }
        return  app("json")->success(compact("info"));
    }
}
